
package com.xnp.reader;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xnp.reader.dummy.DummyContent;
import com.xnp.reader.service.ReadingService;

/**
 * A fragment representing a single Item detail screen. This fragment is either
 * contained in a {@link ItemListActivity} in two-pane mode (on tablets) or a
 * {@link ItemDetailActivity} on handsets.
 */
public class ItemDetailFragment extends Fragment {
    /**
     * The fragment argument representing the item ID that this fragment
     * represents.
     */
    public static final String ARG_ITEM_ID = "item_id";

    /**
     * The dummy content this fragment is presenting.
     */
    private DummyContent.DummyItem mItem;

    private TextView mlTextView;
    private int iCount = 0;
    private int iMS = 0;
    private static final int REFRESH = 1001;
    private MyReceiver mReceiver;

    /**
     * Mandatory empty constructor for the fragment manager to instantiate the
     * fragment (e.g. upon screen orientation changes).
     */
    public ItemDetailFragment() {
    }

    Handler mRefreshHandler = new Handler() {
        public void handleMessage(Message msg) {

            if (iMS < iCount) {
                iMS += 100;
                mlTextView.setText(iMS + "ml");
                mRefreshHandler.sendEmptyMessageDelayed(0, 100);
            } else {
                iMS = 0;
            }

        }
    };

    public class MyReceiver extends BroadcastReceiver {

        @Override
        public void onReceive(Context arg0, Intent arg1) {
            // TODO Auto-generated method stub
            if (arg1.getAction().equals(ReadingService.SEND_ACTION)) {
                int i = arg1.getIntExtra(ReadingService.SEND_COUNT, 0);
                if (i != 0) {
                    iCount = i;
                    mRefreshHandler.sendEmptyMessage(REFRESH);
                }
            }
        }

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getArguments().containsKey(ARG_ITEM_ID)) {
            // Load the dummy content specified by the fragment
            // arguments. In a real-world scenario, use a Loader
            // to load content from a content provider.
            mItem = DummyContent.ITEM_MAP.get(getArguments().getString(ARG_ITEM_ID));
        }
        mReceiver = new MyReceiver();
        IntentFilter filter = new IntentFilter(ReadingService.SEND_ACTION);
        getActivity().registerReceiver(mReceiver, filter);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_item_detail, container, false);

        // Show the dummy content as text in a TextView.
        if (mItem != null) {
            ((TextView) rootView.findViewById(R.id.item_detail)).setText(mItem.content);
        }

        mlTextView = (TextView) rootView.findViewById(R.id.item_detail);
        return rootView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        getActivity().unregisterReceiver(mReceiver);
    }
}
